var express =require('express');
var graphqlHTTP =require('express-graphql');
var{ buildSchema }=require('graphql');
/*
type Query 定义查询
type Account 自定义类型
type Mutation 修改数据需要Mutation
 */
var schema =buildSchema(`
	type Account {
		id: ID
		name: String
		age: Int
	}
	type Query {
		hello: String
		account: Account
		getStudents(id: Int!): String
	}
	input AccountInput {
		name: String
		age: Int
	}
	type Mutation {
		createAccount(input: AccountInput): Account
		updateAccount(id: ID!, input: AccountInput): Account
	}
`),
data = [],id=0;
// 定义方法
var root ={
	hello:()=>{return 'Hello world!'},
	account:()=>{
		return{
			name: 'tom',
			age: 18
		}
	},
	getStudents({id}) {
		return id===1?'tom':'marry'
	},
	createAccount({input}){
		input.id = id++
		data.push(input);
		console.log(JSON.stringify(data))
		return input
	},
	updateAccount({id, input}){
		// data[id] = {...data[id],...input};
		Object.assign(data[id],input)
		console.log(JSON.stringify(data))
		return data[id]
	}
};
var app =express();
app.use('/graphql',graphqlHTTP({
	schema: schema,
	rootValue: root,
	graphiql:true
}));
app.use(express.static('public'));
app.listen(4000);
console.log('Running a GraphQL API server at http://localhost:4000/graphql');
/*
查询
query Query {
  hello
}
新增
mutation {
  createAccount(input: {
    name: "tom"
    age: 18
  }){
    name
    age
  }
}
修改
mutation {
  updateAccount(id: 0,input: {
    name: "tom"
    age: 28
  }){
    name
    age
  }
}
 */